

package com.hazelcast.config;

import com.hazelcast.internal.config.ConfigDataSerializerHook;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.merge.PutIfAbsentMergePolicy;
import com.hazelcast.spi.merge.SplitBrainMergePolicy;

import java.io.IOException;
import java.util.Objects;

import static com.hazelcast.internal.util.Preconditions.checkHasText;
import static com.hazelcast.internal.util.Preconditions.checkPositive;

/**
 * Configuration for {@link SplitBrainMergePolicy}.
 */
public class MergePolicyConfig implements IdentifiedDataSerializable {

    /**
     * Default merge policy.
     */
    public static final String DEFAULT_MERGE_POLICY = PutIfAbsentMergePolicy.class.getName();

    /**
     * Default batch size.
     */
    public static final int DEFAULT_BATCH_SIZE = 100;

    private String policy = DEFAULT_MERGE_POLICY;
    private int batchSize = DEFAULT_BATCH_SIZE;

    public MergePolicyConfig() {
    }

    public MergePolicyConfig(String policy, int batchSize) {
        setPolicy(policy);
        setBatchSize(batchSize);
    }

    public MergePolicyConfig(MergePolicyConfig mergePolicyConfig) {
        this.policy = mergePolicyConfig.policy;
        this.batchSize = mergePolicyConfig.batchSize;
    }

    /**
     * Returns the class name of the {@link SplitBrainMergePolicy}.
     *
     * @return the class name of the merge policy
     */
    public String getPolicy() {
        return policy;
    }

    /**
     * Sets the class name of your {@link SplitBrainMergePolicy}.
     * <p>
     * For the out-of-the-box merge policies the simple class name
     * is sufficient, e.g. {@code PutIfAbsentMergePolicy}.
     * But also the fully qualified class name is fine, e.g.
     * com.hazelcast.spi.merge.PutIfAbsentMergePolicy. For a
     * custom merge policy the fully qualified class name is needed.
     * <p>
     * Must be a non-empty string. The default
     * value is {@code PutIfAbsentMergePolicy}.
     *
     * @param policy the class name of the merge policy
     * @return this {@code MergePolicyConfig} instance
     */
    public MergePolicyConfig setPolicy(String policy) {
        this.policy = checkHasText(policy, "Merge policy must contain text!");
        return this;
    }

    /**
     * Returns the batch size, which will be used to determine
     * the number of entries to be sent in a merge operation.
     *
     * @return the batch size
     */
    public int getBatchSize() {
        return batchSize;
    }

    /**
     * Sets the batch size, which will be used to determine
     * the number of entries to be sent in a merge operation.
     * <p>
     * Must be a positive number. Set to {@code 1} to disable
     * batching. The default value is {@value #DEFAULT_BATCH_SIZE}.
     *
     * @param batchSize the batch size
     * @return this {@code MergePolicyConfig} instance
     */
    public MergePolicyConfig setBatchSize(int batchSize) {
        this.batchSize = checkPositive("batchSize", batchSize);
        return this;
    }

    @Override
    public int getFactoryId() {
        return ConfigDataSerializerHook.F_ID;
    }

    @Override
    public int getClassId() {
        return ConfigDataSerializerHook.MERGE_POLICY_CONFIG;
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        out.writeString(policy);
        out.writeInt(batchSize);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        policy = in.readString();
        batchSize = in.readInt();
    }

    @Override
    public final boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MergePolicyConfig that)) {
            return false;
        }

        if (batchSize != that.batchSize) {
            return false;
        }
        return Objects.equals(policy, that.policy);
    }

    @Override
    public final int hashCode() {
        int result = policy != null ? policy.hashCode() : 0;
        result = 31 * result + batchSize;
        return result;
    }

    @Override
    public String toString() {
        return "MergePolicyConfig{" + "policy='" + policy + '\'' + ", batchSize=" + batchSize + '}';
    }
}
